package cn.zongtilei;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.isever.IStudentService;
import cn.lei.PageBean;
import cn.lei.Student;
import cn.lei.User;
import cn.severimpl.StudentServiceImpl;

public class StudentServlet extends HttpServlet {
	private IStudentService studentService = new StudentServiceImpl();

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");	//post方法解决乱码
		
		HttpSession session = req.getSession();
		User user = (User) session.getAttribute("user");
		if (user==null) {
			//用户没有登陆，重定向到登陆界面
			resp.sendRedirect(req.getContextPath()+"/login.jsp");
			return;
		}
		
		String method = req.getParameter("method");
		switch (method) {
		case "list":
			list(req, resp);
			break;
		case "pageList":
			pageList(req, resp);
			break;
		case "insert":
			insert(req, resp);
			break;
		case "deleteById":
			deleteById(req, resp);
			break;
		case "toUpdate":
			toUpdate(req, resp);
			break;
		case "update":
			update(req, resp);
			break;
		case "deleteAll":
			deleteAll(req, resp);
			break;
		default:
			break;
		}
	}

	private void deleteAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String[] selectIds = req.getParameterValues("selectIds");
		System.out.println(Arrays.toString(selectIds));
		studentService.deleteAll(selectIds);
		resp.sendRedirect(req.getContextPath() + "/student?method=pageList");
	}

	private void pageList(HttpServletRequest req, HttpServletResponse resp) {
		// TODO Auto-generated method stub
		// 1.接受请求参数，封装成对象
		String pagenomber = req.getParameter("pageNo");
		if (pagenomber == null || pagenomber.equals("")) {
			pagenomber = "1";
		}

		int pageNo = Integer.parseInt(pagenomber);
		String pageSi = req.getParameter("pageSize");
		if (pageSi == null || pageSi.equals("")) {
			pageSi = "2";
		}
		int pageSize = Integer.parseInt(pageSi);
		// 2.封装成pageBean，调用service层业务逻辑
		PageBean pageBean = studentService.getpageBean(pageNo, pageSize);
		System.out.println(pageBean);
		// 3.控制界面的跳转
		req.setAttribute("pageBean", pageBean);
		try {
			req.getRequestDispatcher("student_list.jsp").forward(req, resp);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {

		Integer id = Integer.parseInt(req.getParameter("id"));
		String name = req.getParameter("name");
		Integer age = Integer.parseInt(req.getParameter("age"));
		String gender = req.getParameter("gender");
		Student student = new Student(id,name, age, gender);
		// 2.调用service处理-
		boolean result = studentService.update(student);
		System.out.println(result ? "成功" : "失败 ");
		// 3.转发或者重定向到列表界面
		resp.sendRedirect(req.getContextPath() + "/student?method=pageList"); // 充定向
																				// 这也是告诉浏览器让它再发一个请求
	}

	private void toUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Integer id = Integer.parseInt(req.getParameter("id"));
		Student student = studentService.findById(id);
		// 转发到student_update.jsp页面
		req.setAttribute("student", student);
		req.getRequestDispatcher("/student_update.jsp").forward(req, resp);
	}

	private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		// 1、得到浏览器传递过来的参数信息
		Integer id = Integer.parseInt(req.getParameter("id"));
		// 2、调用service处理
		boolean result = studentService.deleteById(id);
		System.out.println(result);
		// 3、重定向到列表界面
		resp.sendRedirect(req.getContextPath() + "/student?method=pageList");

		// 转发
		// req.getRequestDispatcher("/student_list.jsp").forward(req, resp);
	}

	private void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String name = req.getParameter("name");
		Integer age = Integer.parseInt(req.getParameter("age"));
		String gender = req.getParameter("gender");
		Student student = new Student(name, age, gender);
		// 2.调用service处理
		boolean result = studentService.insert(student);
		System.out.println(result ? "成功" : "失败");
		// 3.转发或者重定向到列表界面
		resp.sendRedirect(req.getContextPath() + "/student?method=pageList"); // 充定向
																				// 这也是告诉浏览器让它再发一个请求
	}

	private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String name = req.getParameter("name");
		List<Student> list = new ArrayList<>();
		if (name == null || name.equals("")) {
			list = studentService.list();
		} else {

			// 2.调用service处理
			list = new ArrayList<>();
			list = studentService.findByName(name);
		}
		// 3.转发或者重定向
		req.setAttribute("list", list);
		for (Student student : list) {
			System.out.println(student);
		}

		// 将list数据放到里面
		req.setAttribute("list", list);
		// 转发到student_list.jsp页面进行展示
		req.getRequestDispatcher("/student_list.jsp").forward(req, resp); // 格式
	}

}
